package com.example.qrmenu;
import java.util.Iterator;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {
	private static final String USERNAME = null;
	final Context myApp = this;
	public static ProgressDialog pd;
	DBHandler db ;

	// protected TextView title;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		db = new DBHandler(this);
		if(!userPresent()){
			callWebView();
		} else {
			//FIXME directly start next activity
		}
		
		
		//FIXME check if database exists, if it does not exist call webview, create new database and table. if not display dash board  
	
	}


	private boolean userPresent() {
		List<UserStatusInfo> users = db.getAllUsers();
		if( users!=null){
			Iterator<UserStatusInfo> iter = users.iterator();
			while(iter.hasNext()){
				UserStatusInfo temp = iter.next();
				if( temp.getUsername() == USERNAME){
					return true;
				}
			}
		}
		return false;
	}

	private void callWebView (){
		WebView wv = (WebView) findViewById(R.id.webview);

		wv.getSettings().setJavaScriptEnabled(true);
		wv.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
		wv.setWebViewClient(new WebViewClient() {

			@Override
			public void onPageFinished(WebView view, String url) {

				// Log.d("url: " + url, "onPageFinished");
				// when login is complete, the url will be login_result.ashx?r=0
				if (url.indexOf("/api/login/login_result.ashx") > 0) {
					// When login is successful, there will be a &r=0 in the
					// url. It also means the return data is the token itself.
					if (url.indexOf("&r=0") > 0) {
						// Log.d("success", "onPageFinished");
						// Log.i("onPageFinished - before loading javascript",
						// "");
						Log.i("rohit","inside page finished");
//						view.setBackgroundColor(Color.BLACK);
						view.loadUrl("javascript:window.HTMLOUT.processHTML(document.getElementsByTagName('body')[0].innerHTML);");
					}
				}
			}
		});
		wv.loadUrl("https://ivle.nus.edu.sg/api/login/?apikey=W7RtgW7GZKUpaEkJ1NwUD");
		// wv.loadUrl("https://aces01.nus.edu.sg/SrcvcbWeb/jsp/general/logon.jsp");
	}

	class MyJavaScriptInterface {
		@JavascriptInterface
		public void processHTML(String html) {
			// FIXME is this the access token ?
			Log.i("rohit","in javascript"+html);
			Intent intent = new Intent();
			Log.d("rohit:","Inserting into database");
			db.addUser(new UserStatusInfo("Ravi", html));  
	        UserStatusInfo user = db.getUser("Ravi");
	        Log.i("rohit","acces token of user Ravi"+user.getAccessToken());
			intent.setClass(myApp, LoggedInScreen.class);
			startActivity(intent);
		}

	}

}
